<template>
  <!-- 放置弹层组件 -->

  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="showDialog" @close="closeDialog" @open="handpartment">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in retlist" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click.native="addSubmit">确定</el-button>
      <el-button size="small" @click.native="resetting">取消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { departMentsAPI, adddepartMensAPI, getDepartDetailApi, getXiuGaiAPI } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    notdata: {
      type: Object,
      default: () => {}
    },
    listcode: {
      type: Array,
      default: () => []
    }
  },
  // 需要传入一个props变量来控制 显示或者隐藏
  data() {
    return {
      retlist: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '',
        pid: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: this.checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: this.checkNameRepea, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    closeDialog() {
      // 子传父, 通知到父组件, 将布尔值改成false
      this.$emit('update:showDialog', false)
      this.resetting()
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
    },
    /**
     * 校验部门名称不能和同级别一样
     */
    checkNameRepeat(rule, value, callback) {
      if (this.form.id) {
        if (this.notdata.name === value) {
          callback()
          return
        } else {
          const children = this.listcode.filter(item => item.name === this.notdata.name)
          // console.log(value
          console.log(children)
          const flag = children.some(item => item.name === value)
          // 判断进行校验
          flag ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
        }
      } else {
        const children = this.notdata.children || []
        const flag = children.some(item => item.name === value)
        // 判断进行校验
        flag ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
      }
    },
    /**
     * 判断是否部门编码是否有同名
     */
    checkNameRepea(rule, value, callback) {
      if (this.notdata.code === value) {
        callback()
        return
      } else {
        // console.log(this.codedata)
        const fla = this.listcode.some(item => item.code === value)
        // console.log(fla)
        fla ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
      }
    },
    async handpartment() {
      const { data: res } = await departMentsAPI()
      console.log(res)
      this.retlist = res
    },
    /**
     * 提交表单，兜底验证
     */
    addSubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          const res = await getXiuGaiAPI(this.form)
          console.log(res)
          this.$message.success('修改成功')
        } else {
          this.form.pid = this.notdata.id
          await adddepartMensAPI(this.form)
          this.$message.success('添加成功')
          // console.log(this.$parent)
        }
        this.$parent.DeactivatedList()
        this.closeDialog()
      })
    },
    /**
     * 点击取消或者叉号，清空表单
     */
    resetting() {
      this.$refs.deptForm.resetFields()
    },
    /**
     * 回显数据
     */
    async bianji() {
      const res = await getDepartDetailApi(this.notdata.id)
      console.log(res)
      this.form = res.data
    }
  }
}
</script>

<style>

</style>
